// index.js
// 获取应用实例
import profile from '../../profile.js'

const app = getApp()

Page({
  data: {
    lang: app.globalData.language,
    index: profile.index,
    indexClassNav: profile.index.indexClassNav,
    indexClassSwiper: profile.index.indexClassSwiper1,
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 500,
    indicatorColor: 'rgba(255,255,255,.5)',
    indicatorActiveColor: '#fff',
    popShow: false,
    currentTab: 0,

    isShow: false,

    noticeText: '闭馆通知  尊敬的观众朋友们：为配合空间维护，大华银行艺术空间将于2025年7月3日（周四）12:00至7月4（周五）全天临时闭馆，7月5日（周六）恢复正常开放。给您带来不便，敬请理解！ 大华银行艺术空间 2025年6月24日    Notice of Closure  Dear visitors,The UOB Art Space will be closed from 12:00 on 3 to 4 July 2025 for the exhibition and space upgrades. Regular hours will resume on 5 July(Saturday) 2025.We apologise for any inconvenience. UOB Art Space 24 June 2025',
      scrollLeft: 0,
      contentWidth: 0,
      containerWidth: 0,
      intervalTime: 10, // 滚动间隔时间（毫秒），越小越快
      scrollStep: 2     // 每次滚动步长（像素），越大越快
  },

  // 首页艺术课程全部
  indexClassTap: function (e) {
    let tab = this.data.currentTab
    app.globalData.indexClassTabIndex = tab
    //console.log('tab:'+ tab);
  },

  onLoad() {
    this.initScroll();

    this.setData({
      indexClassSwiper: this.data.index.indexClassSwiper1
    })
  },

  initScroll() {
    const query = this.createSelectorQuery();
    const container = query.select('.notice-container');
    const content = query.select('.notice-content');
    container.boundingClientRect(res => {
      const containerWidth = res.width * 2.2;
      this.setData({
        scrollLeft: containerWidth,
        containerWidth: containerWidth
      });

    }).exec();

    content.boundingClientRect(res => {
      const contentWidth = res.width;
        this.setData({
          contentWidth: contentWidth
        });

        this.startScroll();
    }).exec();
  },

  startScroll() {
    this.scrollInterval = setInterval(() => {
      let newLeft = this.data.scrollLeft - this.data.scrollStep;
      const containerWidth = this.data.containerWidth;

      // 当滚动到第一个内容结束时重置位置
      if (newLeft <= -this.data.contentWidth * 2.2) {
        newLeft = containerWidth;
      }

      this.setData({
        scrollLeft: newLeft
      });
    }, this.data.intervalTime);
  },

  // 弹层事件
  popBtnTap: function (e) {
    this.setData({
      popShow: false
    })
  },
  // 语言切换事件
  menuTap: function (e) {
    this.setData({
      lang: e.detail.lang
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '大华银行艺术探索'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'UOB Art Explorer'
      })
    }
  },

  // 艺术课程swiper切换事件
  classSwiperChange: function (e) {
    let list = this.data.indexClassNav;
    for (let i=0;i<list.length;i++){
      list[i].onthis = false
    }
    list[e.detail.current].onthis = true
    this.setData({
      indexClassNav: list
    })
  },

  // 课程tab切换事件
  changeClassNav: function (e) {
    let id = e.detail.id, list = this.data.indexClassNav;
    for (let i=0;i<list.length;i++){
      if (list[i].id == id) {
        list[i].onthis = true
      }else{
        list[i].onthis = false
      }
    }
    this.setData({
      currentTab: id,
      indexClassNav: list
    })
  },

  onShow: function () {
    this.setData({
      lang: app.globalData.language
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '大华银行艺术探索'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'UOB Art Explorer'
      })
    }
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
        lang: app.globalData.language
      })
    }
  },
})
